package com.hillinsight.controller;

import com.hillinsight.entity.HiveColumns;
import com.hillinsight.entity.HiveTBLS;
import com.hillinsight.entity.MySQLColumns;
import com.hillinsight.entity.MySQLTables;
import com.hillinsight.service.HiveService;
import com.hillinsight.service.MySQLService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by fangzheng on 2018/9/4.
 */
@Controller
@RequestMapping("/meta")
public class MetaController {

    @Autowired
    private MySQLService mySQLService;

    @Autowired
    private HiveService hiveService;


    @RequestMapping(value = "getAllTables",method = RequestMethod.GET)
    public ModelAndView getAllTables(String sourceKey,String datasource_name){
        ModelAndView modelAndView = null;
        if (sourceKey.equalsIgnoreCase("hive")){
            modelAndView = getHiveAllTables();
        } else if (sourceKey.equalsIgnoreCase("mysql")){
            modelAndView = getMySQLAllTables();
        }
        modelAndView.addObject("db_source",sourceKey);
        modelAndView.addObject("db_name",datasource_name);
        return modelAndView;
    }

    @RequestMapping(value = "viewTable",method = RequestMethod.GET)
    public ModelAndView viewTable(String db_source,String table_name,String db_name){
        ModelAndView modelAndView = null;
        if (db_source.equalsIgnoreCase("hive")){
            modelAndView = viewHiveTables(table_name);
        } else if (db_source.equalsIgnoreCase("mysql")){
            modelAndView = viewMySQLTables(table_name);
        }
        modelAndView.addObject("db_source",db_source);
        modelAndView.addObject("db_name",db_name);
        return modelAndView;
    }
    private ModelAndView viewMySQLTables(String table_name){
        ModelAndView modelAndVie = new ModelAndView();
        List<MySQLColumns> columns = mySQLService.getAllColumns(table_name);
        modelAndVie.addObject("columns",columns);
        modelAndVie.setViewName("view-mysql-table");
        return modelAndVie;
    }
    private ModelAndView viewHiveTables(String table_name){
        ModelAndView modelAndVie = new ModelAndView();
        List<HiveColumns> columns = hiveService.getAllColumns(table_name);
        modelAndVie.addObject("columns",columns);
        modelAndVie.setViewName("view-hive-table");
        return modelAndVie;
    }
    private ModelAndView getMySQLAllTables(){
        ModelAndView modelAndView = new ModelAndView();
        List<MySQLTables> allTables = mySQLService.getAllTables();
        modelAndView.addObject("tables",allTables);
        modelAndView.setViewName("mysql-all-tables");
        return modelAndView;
    }
    private ModelAndView getHiveAllTables(){
        List<HiveTBLS> allTables = hiveService.getAllTables();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("tables",allTables);
        modelAndView.setViewName("hive-all-tables");
        return modelAndView;
    }

}
